// 用户模块
import { getUserApi, getUserInfoApi, editProfileApi } from '@/api/user'
import { EditUser, UserAction } from '@/types/user'
import { Dispatch } from 'redux'
// 获取用户个人信息
export const getUserSync = () => {
  return async (dispatch: Dispatch) => {
    // 调接口获取个人信息
    const { data } = await getUserApi()
    dispatch({
      type: 'GET_USER',
      payload: data,
    }) as UserAction
  }
}

// 获取个人基本资料
export const getUserInfoSync = () => {
  return async (dispatch: Dispatch) => {
    // 调接口获取个人基本资料
    const { data } = await getUserInfoApi()
    dispatch({
      type: 'GET_USERINFO',
      payload: data,
    }) as UserAction
  }
}

// 编辑自己的资料(昵称和介绍)
export const editUserSync = (obj: EditUser) => {
  return async (dispatch: Dispatch) => {
    // 编辑资料
    await editProfileApi(obj)
    dispatch({
      type: 'UPDATE_USER',
      payload: obj,
    }) as UserAction
  }
}

// 编辑个人基本资料(生日和性别、简介、昵称)
export const editUserInfoSync = (data: EditUser) => {
  return async (dispatch: Dispatch) => {
    // 编辑资料
    await editProfileApi(data)
    dispatch({
      type: 'UPDATE_USERINFO',
      payload: data,
    }) as UserAction
  }
}

// 编辑头像
export const editPhoto = (data: {
  id: string // 调接口获取个人基本资料
  // 调接口获取个人基本资料
  photo: string
}): UserAction => {
  return {
    type: 'UPDATE_AVATAR',
    payload: data,
  }
}

// 清除用户信息
export const delProfile = (): UserAction => ({
  type: 'DELETE_PROFILE',
})
